package com.toe.common.censor;

import com.toe.common.utils.HttpUtil;
import org.springframework.context.annotation.Configuration;

/**
 * 文本审核接口
 */
@Configuration
public class TextCensor {

    public static String TextCensor(String message) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
        try {
            String param = "text=" + message;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.c6dd4add3de69c7319bf0d024ac69cfd.2592000.1665834755.282335-27043482";

            String result = HttpUtil.post(url, accessToken, param);
            if (result.contains("data")){
                System.out.println("违规了!!!!");
//                String[] msgs = result.split("msg");
//               // System.out.println("msgs:"+msgs[1]);
//                String[] split2 = msgs[1].split("\"keyword\":\"");
//                //System.out.println("split2:"+split2[1]);
//                String[] split3 = split2[1].split("\"");
                return "内容存在违规发言！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
/**
 {"conclusion":"不合规","log_id":16606499079389988,
 "data":[{"msg":"存在低俗辱骂不合规","conclusion":"不合规",
 "hits":[{"wordHitPositions":[{"positions":[[1,2]],
 "label":"500200","keyword":"傻逼"}],"probability":1.0,"
 datasetName":"百度默认文本反作弊库","words":["傻逼"],
 "modelHitPositions":[[0,2,1.0]]}],"subType":5,"conclusionType
 ":2,"type":12}],"isHitMd5":false,"conclusionType":2}
 */
    public static void main(String[] args) {
        String message =  "我日你妈";
        String s = TextCensor.TextCensor(message);
        System.out.println(s);
    }
}
